import { RoleCamp, RoleType } from "../../Interfaces/IEnum";
import {
  IElmentRoot,
  IRoleGenerator,
  IRoleManager,
} from "../../Interfaces/IRole";
import { IDictionary } from "../../Interfaces/IUtils";
import { IdGenerator } from "../../Utils/IdGenerator";

/**
 * 角色管理器实现
 * TODO: 需要自行继承实现 角色添加
 */
export class RoleManager implements IRoleManager {
  //主角
  player: IElmentRoot;

  roleRegister: IDictionary<RoleType, IRoleGenerator>;
  container: IDictionary<number, IElmentRoot>;

  create(
    id: number,
    roleType: RoleType,
    roleCamp: RoleCamp,
    roleParam: RoleCamp,
    roleNo: number
  ): IElmentRoot {
    if (roleNo == 0) roleNo = IdGenerator.getId(RoleType.player);

    //获取角色生成器
    var roleGenerator = this.roleRegister.get(roleType);
    if (roleGenerator == null) {
      console.debug("role type not exist!");
      return null;
    }
    var role: IElmentRoot = roleGenerator.create(
      id,
      roleCamp,
      roleParam,
      roleNo
    );
    this.add(role);
    return role;
  }
  /**注册角色生成器 */
  regisertCreate(roleType: RoleType, roleGenerator: IRoleGenerator) {
    this.roleRegister.set(roleType, roleGenerator);
  }
  add(role: IElmentRoot) {
    if (!role) {
      console.debug("role does not exist!");
    } else if (this.container.get(role.no)) {
      console.debug("role already exist!");
    } else {
      this.container.set(role.no, role);
    }
  }
  remove(role: IElmentRoot) {
    if (role) {
      role.destroy();
      this.container.remove(role.no);
    } else {
      console.debug("role does not exist");
    }
  }
  removeById(no: number) {
    if (this.container.containsKey(no)) {
      this.container.remove(no);
    } else {
      console.debug("role does not exist");
    }
  }
  getRoleById(no: number): IElmentRoot {
    if (this.container.containsKey(no)) {
      return this.container.get(no);
    } else {
      console.debug("role does not exist!");
      return null;
    }
  }
  update() {
    this.container.values.forEach((element) => {
      element.update();
    });
  }
}
